﻿using OA.Core;
using OA.Core.Helper;
using OA.Model.MyAttribute;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OA.Model
{
    [Table("CustomForm")]
    public class CustomForm : BaseInfo
    {
        public int Status { get; set; }

        /// <summary>
        /// 分组ID
        /// </summary>
       // [Index("FormGroup")]
        [Display(Name = "分组")]
        [Tag(TagEnum.Select, "required", "分组","")]
        public int FormGroupID { get; set; }

        /// <summary>
        /// 单据名称
        /// </summary>
        [Display(Name = "单据名称")]
        [MaxLength(10)]
        [Required]
        [Tag(TagEnum.Text, "required", "单据名称", 3, 10)]
        public string Name { get; set; }


        /// <summary>
        /// 数据库表名称
        /// </summary>
        [Display(Name = "表名称")]
        [MaxLength(10)]
        [Tag(TagEnum.Text, "required", "表名称", 3, 50)]
        public string TableName { get; set; }

        /// <summary>
        /// 编号前缀
        /// </summary>
        [Required]
        [MaxLength(20)]
        [Display(Name = "编号前缀")]
        [Tag(TagEnum.Text, "required", "编号前缀", 2, 10)]
        public string NoPrefix { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [Display(Name = "排序")]
        [Tag(TagEnum.Number, "required", "排序", 0, 100)]
        public int Sort { get; set; }


        /// <summary>
        /// 扩展字段
        /// </summary>
        [Display(Name = "扩展字段")]
        public string ExtendField { get; set; }         

        /// <summary>
        /// 单据图标
        /// </summary>
        [Display(Name = "单据图标")]
        [MaxLength(100)]
        [Tag(TagEnum.SingleImageUpload, "required", "单据图标", 1,100)]
        public string Ico { get; set; }         


        

        [Display(Name = "单据说明")]
        [Tag(TagEnum.TextArea, "", "单据说明", 3, 200)]
        public string Explain { get; set; }    

        /// <summary>
        /// 通知模版
        /// </summary>
        [Display(Name = "通知模版")]
        [MaxLength(200)]
        [Tag(TagEnum.TextArea, "", "通知模版", 3, 200)]
        public string NotificationTemplate { get; set; }

        /// <summary>
        /// 通知成员
        /// </summary>
        [Display(Name = "通知成员")]
        public string NotificationMember { get; set; }


        /// <summary>
        /// 打印模版
        /// </summary>
        public string PrintTemplate { get; set; }

        /// <summary>
        /// 自定义流程
        /// </summary>
        [Display(Name = "自定义流程")]
        [Tag(TagEnum.Switch, "", "自定义流程", 3, 3)]
        public bool IsCustomProcess { get; set; } = false;

        /// <summary>
        /// 自定义打印
        /// </summary>
        [Display(Name = "自定义打印")]
        [Tag(TagEnum.Switch, "", "自定义打印", 3, 3)]
        public bool IsCustomView { get; set; } = false;


        [Display(Name = "横向打印")]
        [Tag(TagEnum.Switch, "", "横向打印", 3, 3)]
        public bool HorizontalPrinting { get; set; } = false;


        [NotMapped]
        public List<ExtendField> ListExtendField { get; set; }

        [NotMapped]
        public List<ExtendField> TableFields { get; set; }

        public virtual FormGroup FormGroup { get; set; }

        /// <summary>
        /// 打印方向 1：竖向  2：横向
        /// </summary>
        public int PdfPageOrientation { get; set; }

        /// <summary>
        /// 回调Url，多个回调使用逗号隔开
        /// </summary>
        [Display(Name = "回调Url")]
        [MaxLength(1000)]
        [Tag(TagEnum.TextArea, "", "回调Url", 1, 1000)]
        public string CallBackUrl { get; set; }

        /// <summary>
        /// 列表页面配置
        /// </summary>
        public string ListInfoContent { get; set; }        

        [NotMapped]
        public ListInfo ListInfo { get { return JsonHelper.DeserializeObj<ListInfo>(ListInfoContent); } }

        public string ApprovalProcessNodeContent { get; set; }

    }

    public class ListInfo
    {

        /// <summary>
        /// 过滤字段
        /// </summary>
        public class SelectField
        {
            public string Name { get; set; }             

            /// <summary>
            /// 排序的顺序
            /// </summary>
            public int OrderIndex { get; set; }
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        public class OrderField
        {
            public string Name { get; set; }

            /// <summary>
            /// 0:顺序 1:倒序
            /// </summary>
            public int OrderType { get; set; }

            /// <summary>
            /// 排序的顺序
            /// </summary>
            public int OrderIndex { get; set; }
        }

        /// <summary>
        /// 显示字段
        /// </summary>
        public List<string> DisplayField { get; set; }
    }

    public enum PrintModeType
    {
        [Description("竖向")]
        Varertical = 1,

        [Description("横向")]
        Landscape = 2
    }

    /// <summary>
    /// 表单设计结果
    /// </summary>
    public class CustomFormExtendInfo
    {
        public List<ExtendField> ListExtendField { get; set; }

        public List<ExtendField> TableFields { get; set; }
    }

    public class TableValues
    {
        public string Attribute { get; set; }
        public List<Dictionary<string, string>> List { get; set; }
    }

    public class SimpleCustomForm : SimpleInfo
    {
        public string Name { get; set; }

        public int Sort { get; set; }

        public string ApprovalTreeName { get; set; }

        public int CustomFormID { get; set; }

        public bool IsCustomView { get; set; }

        public bool HasProcessingFlow { get; set; }

        public bool HasCountersign { get; set; }

        /// <summary>
        /// 分组名称
        /// </summary>
        public string GroupName { get; set; }

        public static Expression<Func<CustomForm, SimpleCustomForm>> Map = m => new SimpleCustomForm()
        {
            ID = m.ID,
            Name = m.Name,
            Sort = m.Sort,          
            IsCustomView = m.IsCustomView
        };

    }


    public class SimpleCustomForm2 : SimpleInfo
    {
        public string Name { get; set; }

        public int Sort { get; set; }

        public string Ico { get; set; }

        public int FormGroupID { get; set; }

        public string Code
        {
            get
            {
                return "002" + this.FormGroupID.ToString("D3") + this.ID.ToString("D3");
            }
        }

        public static Expression<Func<CustomForm, SimpleCustomForm2>> Map = m => new SimpleCustomForm2()
        {
            ID = m.ID,
            Name = m.Name,
            Sort = m.Sort,
            Ico = m.Ico,
            FormGroupID = m.FormGroupID
        };

    }




}
